Introduction to NODE JS

What is Node Js?

1) Sends the task to the computer's file system.
2) Waits while the file system opens and reads the file.
3)Returns the content to the client.
4)Ready to handle the next request.

Why learn NODEjs?

1) Node.js can create, open, read, write, delete, and close files on the server
2)Node.js can generate dynamic page content.
3) Node.js can collect form data
4) Node.js can add, delete, modify data in your database
5) IT is fun and easy to learn!
6)Node.js files contain tasks that will be executed on certain events.

Node.js Get Started :

Node.js files must be initiated in the "Command Line Interface" program of your computer. How to open the command line interface on your computer depends on the operating system. For Windows users, press the start button and look for "Command Prompt", or simply write "cmd" in the search field. Navigate to the folder that contains the file "myfirst.js", the command line interface window should look something like this:

Quickstart for NODE js :

Use the module "myfirstmodule" in a Node.js file: var http = require('http'); var dt = require('./ myfirst module'); http.createServer (function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write("The date and time are currently: " + dt.myDateTime()); res.end(); }).listen(8080); NPM is a package manager for Node.js packages, or modules if you like. www.npmjs.com hosts thousands of free packages to download and use. The NPM program is installed on your computer when you install Node.js

Download a Package:

  • Downloading a package is very easy. Open the command line interface and tell NPM to download the package you want.
  • Once the package is installed, it is ready to use. Include the "upper-case" package the same way you include any other module.
  • it has many advantages when it involves programming, All the program stand-alone files must use the foremost function to allow the program to start out up and motivate its functions.
  • it can support unions and structures that are a mix of stand-alone and put-together files, It uses the standard C++ application mentioned as uses the reserved library word mentioned as “goto” that’s the same as Java’s continue, or break commands.
  • Node.js File System Module:

  • The fs.readFile() method is used to read files on your computer. Assume we have the following HTML file (located in the same folder as Node.js): demofile1.html var http = require ('http'); var fs = require ('fs'); http. createServer (function (req, res) { fs.readFile ('demo file1 .html', function (err, data) { res. writeHead(200, {' Content- Type': 'text/ html'}); res. write(data); return res.end(); }); }).listen(8080);
  • Node.js Events :

  • Node.js has a built-in module, called "Events", where you can create-, fire-, and listen for- your own events. To include the built-in Events module use the require() method. In addition, all event properties and methods are an instance of an EventEmitter object. To be able to access these properties and methods, create an EventEmitter object:
  • You can assign event handlers to your own events with the EventEmitter object.
  • Learn about const correctness. Any method that doesn’t alter class state should be marked const, and any reference you’re not going to change should be const.
  • In the example below we have created a function that will be executed when a "scream" event is fired